var dao = require('./dao')

var userDALS = {
    getAllContents:function(cb){
        var sql = 'select * from article order by likeClick desc'
        var params = []
        dao(sql,params,function(err,results){
            if(err){
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },

    getAllContents1:function(cb){
        var sql = 'select * FROM article ORDER BY response desc'
        var params = []
        dao(sql,params,function(err,results){
            if(err){
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    getAllContents2:function(uId,cb){
        var sql = 'select title, content,pTime,likeClick,aImg,response,collect from article a join (select * from concern where uId = ?) u on u.aId = a.aId'

        var params = [uId]
        dao(sql,params,function(err,results){
            if(err){
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    sendOn:function(newUser,cb){
        var sql = 'insert into article(uId,title,content,pTime,likeClick) values(?,?,?,?,1)'
        var params = [newUser.userId,newUser.userTitle,newUser.userContent,newUser.date]
        dao(sql,params,function(err,results){
            if(err){
                console.log(err.message)
                cb(err,null)
            }else{
                cb(null,results)
            }
        })

    },
    addComments:function(Comments,cb){
        // console.log('查询语句')
        var sql = 'insert into articlecomment1(aId,content,uId,pTime,likeClick,response) values(?,?,?,?,0,0)'
        var params = [Comments.aId,Comments.userComment,Comments.userId,Comments.date]
        console.log(params)
        dao(sql,params,function(err,results){
            if(err){
                console.log(err.message)
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    showComments:function(aId,cb){
        var sql = 'select aId,u.uId,content,pTime,likeClick,nickName,headImg from (select * from articlecomment1 WHERE aId=?) a join user u on a.uId=u.uId'
        var params=[aId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    updateLike:function(ups,cb){
        var sql = 'update article set likeClick=? where aId = ?'
        var params=[ups.num,ups.aId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    topic:function(aId,cb){
        var sql = 'select aId,u.uId,content,pTime,likeClick,response,Signature,nickName,headImg,occupation from (select * from articlecomment1 WHERE aId=?) a join user u on a.uId=u.uId'
        var params = [aId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    getAuthor:function(aId,cb){
        var sql='select headImg,nickName from user u join (SELECT uId from article where aId = ?) a on u.uId = a.uId'
        var params = [aId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    focusThis:function(user,cb){
        var sql = 'insert into concern(aId,reason,uId,cTime) values(?,"因为爱情",?,?)'
        var params = [user.aId,user.uId,user.date]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    focusDel:function(user,cb){
        var sql ='delete from concern where aId=? and uId=?'
        var params = [user.aId,user.uId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    showContent:function(cb){
        var sql='select * from specialColom'
        var params = []
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },

    //读取用户信息
    userInfo:function(userId,cb){
        var sql = 'select * form user where uId = ?'
        var params = [userId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    rewrite:function(users,cb){
        console.log(users)
        var sql = `update user set sex = ${users.sex} , birth = '${users.birth}' , mail = '${users.eMail}' , nickName = '${users.nickName}' , Signature = '${users.signature}' , occupation = '${users.occupy}' where uId = ${users.uId} `
        var params = []
        console.log(params)
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })  
    },
    rePicture:function(users,cb){
        console.log(users)
        var sql = `update user set headImg = '${users.picture}' where uId = ${users.uId}`
        var params = []
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })   
    },
    getInfos:function(uId,cb){
        var sql = 'select * from user where uId = ?'
        var params = [uId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    getArticle:function(uId,cb){
        var sql = `select title,content,pTime,likeClick,response,nickName,headImg,Signature from (SELECT * from article where uId = ${uId}) a join user on a.uId = user.uId`
        var params = []
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)    
            }else{
                cb(null,results)
            }
    })
    },
    collect:function(users,cb){
        var sql='insert into concern(aId,uId,cTime) values(?,?,?)'
        var params=[users.aId,users.uId,users.date]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
            })
    },
    showCollect:function(uId,cb){
        var sql = `select title,content,likeClick,response,headImg,nickName,Signature from (select title,content,a.uId,likeClick,response from article a join (select * from concern where uId=${uId}) b on a.aId = b.aId) c join user u on u.uId = c.uId`
        var params = []
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    calLove:function(uId,cb){
        var sql = 'select * from concern where uId = ?'
        var params = [uId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
        })
    },
    delCollect:function(users,cb){
        var sql='delete from concern where aId=? and uId = ?'
        var params=[users.aId,users.uId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
            })
    },
    getArticleInfo:function(uId,cb){
        var sql = 'select * from article where uId = ?'
        var params = [uId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
            })
    },
    addImgs:function(user,cb){
        var sql = 'update article set aImg = ? where aId = ?'
        var params=[user.picture,user.aId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
            })
    },
    addCommentss:function(users,cb){
        var sql  = 'insert into articlecomment1(aId,content,uId,pTime,likeClick,response) values(?,?,?,?,0,0)'
        var params = [users.aId,users.comment,users.uId,users.date]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
            })
    },
    getUserInfo:function(uId,cb){
        var sql = 'select * from user where uId = ?'
        var params = [uId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
            })
    },
    selectConcern:function(uId,cb){
        var sql = 'select headImg,nickName,Signature,title,content,pTime,likeClick from user u join (select title,content,pTime,likeClick,a.uId from article a join (select bConcern from person_concern where uId = ?) u on u.bConcern = a.uId) temp on u.uId = temp.uId'
        var params = [uId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{  
                cb(null,results)
            }
            })
    },
    selectConcernNum:function(uId,cb){
        var sql = 'select * from person_concern where uId = ?'
        var params = [uId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
            })
    },
    selectConcernNums:function(uId,cb){
        var sql = 'select * from person_concern where bConcern = ?'
        var params = [uId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{
                cb(null,results)
            }
            })
    },
    selectConcerned:function(uId,cb){
        var sql = 'select headImg,nickName,Signature,title,content,pTime,likeClick from user u join (select title,content,pTime,likeClick,a.uId from article a join (select uId from person_concern where bConcern = ?) u on u.uId = a.uId) temp on u.uId = temp.uId'
        var params = [uId]
        dao(sql,params,function(err,results){
            if(err){
                console.log('查询错误')
                cb(err,null)
            }else{  
                cb(null,results)
            }
            })
    }
    
}

module.exports = userDALS